package com.hkfn.rfb.common.http.Json;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.bean.RequestModel;
import com.hkfn.rfb.common.utils.JSONUtil;
import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.ToastUtils;

import org.json.JSONObject;

public abstract class ParseJsonData {
    public static final String ERROR_CODE = "code";
    public static final String REQUEST_OK_CODE = "200";
    public static final String RELOGIN_CODE = "401";// 请求未授权
    public static final String ERROR_MSG = "msg";
    public static final String REQUEST_OK = "request_ok";
    public static final String ANALYZE_ERROR_MSG = "数据解析异常";
    public static final String DATA = "data";

    public void parseResponseWithGSON(String jsonData, final BaseActivity activity, Handler handler, int what){
        Message message = handler.obtainMessage();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putBoolean(REQUEST_OK, false);
        bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
        boolean isNeedSendMsg = true;
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            if (jsonObject.has("access_token")){
                bundle.putString(ERROR_CODE, REQUEST_OK_CODE);
                analyzeResult(jsonData, bundle, what);
            }else {
                RequestModel requestModel = JSONUtil.jsonObjectToBean(jsonData, RequestModel.class);
                if (null != requestModel) {
                    bundle.putString(ERROR_CODE, requestModel.getError_code());
                    String error_msg = requestModel.getError_msg();
                    if (!StringUtils.isEmpty(error_msg)) {
                        String code = requestModel.getError_code();
                        if (!(REQUEST_OK_CODE.equals(code))) {
                            final String finalError_msg = error_msg;
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtils.showToast(activity, finalError_msg);
                                }
                            });
                        }
                    } else {
                        error_msg = ANALYZE_ERROR_MSG;
                    }
                    if (REQUEST_OK_CODE.equals(requestModel.getError_code())) {
                        String data = jsonObject.getString(DATA);
                        if (!StringUtils.isEmpty(data)) {
                            analyzeResult(data, bundle, what);
                        } else {
                            bundle.putString(ERROR_MSG, error_msg);
                        }
                    } else if (RELOGIN_CODE.equals(requestModel.getError_code())) {
//                        if (!(activity instanceof LoginActivity)) {
//                            handler.removeCallbacksAndMessages(null);
//                            UserInfoModel.clearUserLoginMessage(activity);
//                            ActivityCollecter.finishAllActivitys();
//                            activity.openActivity(LoginActivity.class);
//                        }

                    } else {
                        bundle.putString(ERROR_MSG, error_msg);
                    }
                } else {
                    bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
                }
            }
        }catch (Exception e){
            bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
        }finally {
            if (isNeedSendMsg) {
                message.setData(bundle);
                handler.sendMessage(message);
            }
        }
    }

    /**
     * 对应数据的具体解析
     * @param dataStr
     * @param bundle
     * @param what
     */
    public abstract void analyzeResult(String dataStr, Bundle bundle, int what);
}
